const App = getApp()
var WxCode = '';
var WxEncryptedData = '';
var WxIv = '';
var sid = 0;
var surl = '';
var fxurl = '/pages/index';//分享地址
Page({
  data: {
    sname: getApp().data.sitename,
    logged: !1
  },
  onLoad(options) {
    if(options.fxurl != undefined)//分享链接
    {
      fxurl = options.fxurl;
    }
  },
  onGotUserInfo: function (e) {//授权回调
    if (e.detail.rawData != undefined) {
      let slet = this;
      wx.login({//login流程
        success: function (res) {//登录成功
          if (res.code) {
            WxCode = res.code;
            wx.getUserInfo({
              success: function (res2) {//获取userinfo成功
                WxEncryptedData = encodeURIComponent(res2.encryptedData);
                WxIv = res2.iv;
                wx.request({
                  url: getApp().data.apiUrl + '/Login.ashx',
                  data: {
                    code: WxCode,
                    encryptedData: WxEncryptedData,
                    iv: WxIv
                  },
                  method: 'GET',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    console.log(res.data);
                    if (res.data.status == 1)//注册成功/未审核
                    {
                      wx.showToast({
                        title: '账号未审核',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                    else if (res.data.status == 2)//已注册
                    {
                      wx.setStorageSync('LoginSessionKey', res.data.signature);
                      getApp().data.SessionKey = res.data.signature;
                      //登陆成功跳转到首页
                      wx.redirectTo({
                        url: fxurl
                      })
                    }
                  }
                })

              },
              fail: function () {//失败

              }
            })
          }
        },
        fail: function () {//失败
        },
        complete: function () {//完成
        }
      })
    }
    else {
      wx.showToast({
        title: '不授权无法使用',
        icon: 'success',
        duration: 1000
      })
    }
  },
  onShow() {
  },
  login() {//确认登陆
    wx.openSetting({//二次获取授权
      success: function (data) {
        let jg = data.authSetting["scope.userInfo"];
        if (jg == true)//拒绝授权
        {
          wx.redirectTo({
            url: '/pages/index'
          })
        }
        else//拒绝授权
        {
          wx.showToast({
            title: '不授权无法使用',
            icon: 'success',
            duration: 1000
          })
        }
      }
    })
  }
})